package day52.org.controller;

import day52.basic.util.ExcelUtil;
import day52.org.domain.Employee;
import day52.org.domain.ShopAuditLog;
import day52.org.service.IEmployeeService;
import day52.org.service.IShopService;
import day52.org.domain.Shop;
import day52.org.query.ShopQuery;
import day52.basic.util.AjaxResult;
import day52.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){

        if( shop.getId()!=null){
            shopService.update(shop);
        }else{
            shopService.add(shop);
        }
        return AjaxResult.me();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        shopService.delete(id);
        return AjaxResult.me();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id) {

        return shopService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query) {

        return shopService.pageQuery(query);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settle")
    public AjaxResult settle(@RequestBody @Valid Shop shop){
        shopService.settle(shop);
        return AjaxResult.me();
    }

    /**
     * 店铺审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        shopService.pass(shopAuditLog);
        return AjaxResult.me();
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.reject(shopAuditLog);
        return AjaxResult.me();
    }

    /**
     * 导出Excel
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xls", response);
    }

    /**
     * 导入数据
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
//        shops.forEach(shop -> {
//            shopService.add(shop);
//        });
        shopService.batchAdd(shops);

    }

    @GetMapping("/employee/{id}")
    public List<Employee> getEmployee(@PathVariable Long id){
        List<Employee> employees = employeeService.findAdmin(id);
        return employees;
    }

}
